{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "1673d914-f1cb-4308-a48c-a882293c9959",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-06-07T06:25:51.315361Z",
     "iopub.status.busy": "2022-06-07T06:25:51.314937Z",
     "iopub.status.idle": "2022-06-07T06:25:51.320384Z",
     "shell.execute_reply": "2022-06-07T06:25:51.319592Z",
     "shell.execute_reply.started": "2022-06-07T06:25:51.315333Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 导入包\n",
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "4cd9b746-2737-4ba3-842c-dd8eeb8b429e",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-06-07T06:30:23.746026Z",
     "iopub.status.busy": "2022-06-07T06:30:23.745441Z",
     "iopub.status.idle": "2022-06-07T06:30:23.754755Z",
     "shell.execute_reply": "2022-06-07T06:30:23.753506Z",
     "shell.execute_reply.started": "2022-06-07T06:30:23.745991Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    test1@hunnu.edu\n",
       "b    test2@gmail.com\n",
       "c      test3@163.com\n",
       "d                NaN\n",
       "dtype: object"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建实验数据\n",
    "data = pd.Series({\n",
    "    \"a\":\"test1@hunnu.edu\",\n",
    "    \"b\":\"test2@gmail.com\",\n",
    "    \"c\":\"test3@163.com\",\n",
    "    \"d\":np.nan,\n",
    "})\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "3247b298-c073-4df6-8a27-5f7a960669a8",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-06-07T06:30:28.229896Z",
     "iopub.status.busy": "2022-06-07T06:30:28.229478Z",
     "iopub.status.idle": "2022-06-07T06:30:28.275168Z",
     "shell.execute_reply": "2022-06-07T06:30:28.273986Z",
     "shell.execute_reply.started": "2022-06-07T06:30:28.229868Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'float' object has no attribute 'split'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "Input \u001b[0;32mIn [8]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mdata\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mmap\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43;01mlambda\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mx\u001b[49m\u001b[43m:\u001b[49m\u001b[43mx\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msplit\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43m@\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[0;32m/opt/miniconda3/envs/jpt/lib/python3.10/site-packages/pandas/core/series.py:4237\u001b[0m, in \u001b[0;36mSeries.map\u001b[0;34m(self, arg, na_action)\u001b[0m\n\u001b[1;32m   4162\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mmap\u001b[39m(\u001b[38;5;28mself\u001b[39m, arg, na_action\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m Series:\n\u001b[1;32m   4163\u001b[0m     \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m   4164\u001b[0m \u001b[38;5;124;03m    Map values of Series according to an input mapping or function.\u001b[39;00m\n\u001b[1;32m   4165\u001b[0m \n\u001b[0;32m   (...)\u001b[0m\n\u001b[1;32m   4235\u001b[0m \u001b[38;5;124;03m    dtype: object\u001b[39;00m\n\u001b[1;32m   4236\u001b[0m \u001b[38;5;124;03m    \"\"\"\u001b[39;00m\n\u001b[0;32m-> 4237\u001b[0m     new_values \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_map_values\u001b[49m\u001b[43m(\u001b[49m\u001b[43marg\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mna_action\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mna_action\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m   4238\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_constructor(new_values, index\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mindex)\u001b[38;5;241m.\u001b[39m__finalize__(\n\u001b[1;32m   4239\u001b[0m         \u001b[38;5;28mself\u001b[39m, method\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mmap\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m   4240\u001b[0m     )\n",
      "File \u001b[0;32m/opt/miniconda3/envs/jpt/lib/python3.10/site-packages/pandas/core/base.py:880\u001b[0m, in \u001b[0;36mIndexOpsMixin._map_values\u001b[0;34m(self, mapper, na_action)\u001b[0m\n\u001b[1;32m    877\u001b[0m         \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mValueError\u001b[39;00m(msg)\n\u001b[1;32m    879\u001b[0m \u001b[38;5;66;03m# mapper is a function\u001b[39;00m\n\u001b[0;32m--> 880\u001b[0m new_values \u001b[38;5;241m=\u001b[39m \u001b[43mmap_f\u001b[49m\u001b[43m(\u001b[49m\u001b[43mvalues\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mmapper\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    882\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m new_values\n",
      "File \u001b[0;32m/opt/miniconda3/envs/jpt/lib/python3.10/site-packages/pandas/_libs/lib.pyx:2870\u001b[0m, in \u001b[0;36mpandas._libs.lib.map_infer\u001b[0;34m()\u001b[0m\n",
      "Input \u001b[0;32mIn [8]\u001b[0m, in \u001b[0;36m<lambda>\u001b[0;34m(x)\u001b[0m\n\u001b[0;32m----> 1\u001b[0m data\u001b[38;5;241m.\u001b[39mmap(\u001b[38;5;28;01mlambda\u001b[39;00m x:\u001b[43mx\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43msplit\u001b[49m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m@\u001b[39m\u001b[38;5;124m\"\u001b[39m))\n",
      "\u001b[0;31mAttributeError\u001b[0m: 'float' object has no attribute 'split'"
     ]
    }
   ],
   "source": [
    "# 因为元素里存在非字符串（NAN），所以直接使用字符串操作会报错\n",
    "data.map(lambda x:x.split(\"@\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "d3cf6384-03b3-43f3-918c-4cea56a34b9a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-06-07T06:30:31.950982Z",
     "iopub.status.busy": "2022-06-07T06:30:31.950543Z",
     "iopub.status.idle": "2022-06-07T06:30:31.959256Z",
     "shell.execute_reply": "2022-06-07T06:30:31.958476Z",
     "shell.execute_reply.started": "2022-06-07T06:30:31.950952Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    [test1, hunnu.edu]\n",
       "b    [test2, gmail.com]\n",
       "c      [test3, 163.com]\n",
       "d                   NaN\n",
       "dtype: object"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 通过pandas数据对象的str属性调用字符串操作可以排查非字符串类型的干扰\n",
    "data.str.split(\"@\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "94a93519-51c7-4d3d-8c38-b595b0537476",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-06-07T06:37:23.253151Z",
     "iopub.status.busy": "2022-06-07T06:37:23.252885Z",
     "iopub.status.idle": "2022-06-07T06:37:23.259369Z",
     "shell.execute_reply": "2022-06-07T06:37:23.258114Z",
     "shell.execute_reply.started": "2022-06-07T06:37:23.253131Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    False\n",
       "b     True\n",
       "c     True\n",
       "d      NaN\n",
       "dtype: object"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 通过pandas数据对象的str属性调用contrains函数\n",
    "data.str.contains(\"com\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "c3968841-1951-446a-9302-b00f579cbd04",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-06-07T06:38:10.102735Z",
     "iopub.status.busy": "2022-06-07T06:38:10.102426Z",
     "iopub.status.idle": "2022-06-07T06:38:10.128821Z",
     "shell.execute_reply": "2022-06-07T06:38:10.127152Z",
     "shell.execute_reply.started": "2022-06-07T06:38:10.102712Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'Series' object has no attribute 'findall'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "Input \u001b[0;32mIn [16]\u001b[0m, in \u001b[0;36m<cell line: 2>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[38;5;66;03m# 因为元素里存在非字符串（NAN），直接使用正则表达式操作会报错\u001b[39;00m\n\u001b[0;32m----> 2\u001b[0m \u001b[43mdata\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mfindall\u001b[49m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m@\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
      "File \u001b[0;32m/opt/miniconda3/envs/jpt/lib/python3.10/site-packages/pandas/core/generic.py:5575\u001b[0m, in \u001b[0;36mNDFrame.__getattr__\u001b[0;34m(self, name)\u001b[0m\n\u001b[1;32m   5568\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m (\n\u001b[1;32m   5569\u001b[0m     name \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_internal_names_set\n\u001b[1;32m   5570\u001b[0m     \u001b[38;5;129;01mand\u001b[39;00m name \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_metadata\n\u001b[1;32m   5571\u001b[0m     \u001b[38;5;129;01mand\u001b[39;00m name \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_accessors\n\u001b[1;32m   5572\u001b[0m     \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_info_axis\u001b[38;5;241m.\u001b[39m_can_hold_identifiers_and_holds_name(name)\n\u001b[1;32m   5573\u001b[0m ):\n\u001b[1;32m   5574\u001b[0m     \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m[name]\n\u001b[0;32m-> 5575\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mobject\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[38;5;21;43m__getattribute__\u001b[39;49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mname\u001b[49m\u001b[43m)\u001b[49m\n",
      "\u001b[0;31mAttributeError\u001b[0m: 'Series' object has no attribute 'findall'"
     ]
    }
   ],
   "source": [
    "# 因为元素里存在非字符串（NAN），直接使用正则表达式操作会报错\n",
    "data.findall(\"@\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "5119a431-d754-4bf0-b8b6-47d9624dc79a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-06-07T06:31:13.036536Z",
     "iopub.status.busy": "2022-06-07T06:31:13.036021Z",
     "iopub.status.idle": "2022-06-07T06:31:13.045252Z",
     "shell.execute_reply": "2022-06-07T06:31:13.044190Z",
     "shell.execute_reply.started": "2022-06-07T06:31:13.036499Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    [@]\n",
       "b    [@]\n",
       "c    [@]\n",
       "d    NaN\n",
       "dtype: object"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 通过pandas数据对象的str属性操作正则表达式，可以排查非字符串类型的干扰\n",
    "data.str.findall(\"@\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "c7daa2f1-15ab-4350-b82f-948b80b9aefb",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-06-07T06:31:58.289521Z",
     "iopub.status.busy": "2022-06-07T06:31:58.288977Z",
     "iopub.status.idle": "2022-06-07T06:31:58.298124Z",
     "shell.execute_reply": "2022-06-07T06:31:58.297099Z",
     "shell.execute_reply.started": "2022-06-07T06:31:58.289450Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    test1\n",
       "b    test2\n",
       "c    test3\n",
       "d      NaN\n",
       "dtype: object"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 通过pandas数据对象的str属性广播字符串操作\n",
    "data.str[:5]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
